package com.lvyh.lightframe.multidatasource.common;

import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of multi data source switching
 *
 * @author lvyh 2021/03/20.
 */

public class MultiDataSourceHolder {

    public static final ThreadLocal<String> multiDataSourceContext = new ThreadLocal<>();

    public static List<String> multiDataSources = new ArrayList<>();

    /**
     * Is it included the data source
     */
    public static boolean contain(String datasourceId) {
        return multiDataSources.contains(datasourceId);
    }

    public static List<String> getMultiDataSources() {
        return multiDataSources;
    }

    /**
     * Gets the name of the data source
     */
    public static String get() {
        return multiDataSourceContext.get();
    }

    /**
     * Switch data sources
     */
    public static void set(String name) {
        multiDataSourceContext.set(name);
    }

    /**
     * Clear current data source
     */
    public static void remove() {
        multiDataSourceContext.remove();
    }

}
